﻿<template>
  <a-modal
    :title="title"
    width="55%"
    :visible="visible"
    :confirmLoading="loading"
    @ok="handleSubmit"
    @cancel="()=>{this.visible=false}"
  >
    <a-spin :spinning="loading">
      <a-form-model ref="form" :model="entity" :rules="rules" v-bind="layout">
        <a-form-model-item label="类别编码" prop="typenum">
          <!-- <a-input v-model="entity.typenum" autocomplete="off" /> -->
          <a-select @change="handleChange" show-search v-model="entity.typenum">
            <a-select-option v-for="(item, index) in DicType" :value="item.Id" :key="index">
              {{ item.name }}
            </a-select-option>
          </a-select>
        </a-form-model-item>
        <!-- <a-form-model-item label="类型名称" prop="typename">
          <a-input v-model="entity.typename" autocomplete="off" />
        </a-form-model-item> -->
        <a-form-model-item label="字典名称" prop="name">
          <a-input v-model="entity.name" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="所属机构" prop="BranchId">
          <!-- <a-input v-model="entity.BranchId" autocomplete="off" /> -->
          <a-select
          @change="servicechange"
                show-search
                v-model="entity.BranchId"
                allowClear
                placeholder="请选择服务商"
              >
                <a-select-option v-for="(item, index) in ServiceList" :key="index" :value="item.Id">
                  {{ item.ServiceName }}
                </a-select-option>
              </a-select>
        </a-form-model-item>
        <a-form-model-item label="排序" prop="sort">
          <a-input v-model="entity.sort" type="Number" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="是否显示" prop="isShow">
          <a-radio-group default-value="1" button-style="solid" v-model="entity.isShow">
            <a-radio-button :value="1">
            启用
            </a-radio-button>
            <a-radio-button :value="0">
            停用
            </a-radio-button>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item label="说明" prop="remark">
          <a-input v-model="entity.remark" type="textarea" :rows="3" autocomplete="off" />
          
        </a-form-model-item>
        
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
export default {
  props: {
    parentObj: Object
  },
  data() {
    return {
      layout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 18 }
      },
      DicType:[],
      visible: false,
      loading: false,
      entity: {},
      rules: {
        typenum: [{ required: true, message: '必填' }],
        name: [{ required: true, message: '必填' }],
        BranchId: [{ required: true, message: '必填' }],
      },
      title: '',
      ServiceList:[]
    }
  },
  methods: {
    init() {
      this.title = "新增"
      this.GetDicType()
      this.GetService();
      this.visible = true
      this.entity = {}
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    openForm(id, title) {
      this.init()

      if (id) {
        this.title = "修改"
        this.loading = true
        this.$http.post('/Base_leasesystem_dictionary_values/leasesystem_dictionary_values/GetTheData', { id: id }).then(resJson => {
          this.loading = false

          this.entity = resJson.Data
        })
      }
    },
    handleSubmit() {
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return
        }
        this.loading = true
        this.$http.post('/Base_leasesystem_dictionary_values/leasesystem_dictionary_values/SaveData', this.entity).then(resJson => {
          this.loading = false

          if (resJson.Success) {
            this.$message.success('操作成功!')
            this.visible = false

            this.parentObj.getDataList()
          } else {
            this.$message.error(resJson.Msg)
          }
        })
      })
    },
    GetDicType(){
      this.$http.post("/Base_Manage/Home/GetDic_type").then(res=>{
        this.DicType = res.Data
      }) 
    },
    GetService() {
      this.$http.post('/Base_Manage/Home/GetService_Select').then((res) => {
        this.ServiceList = res.Data
      })
    },
    handleChange(value){
      let obj = {};
         obj = this.DicType.find((item)=>{ //这里的Data就是上面遍历的数据源
           return item.Id === value; //筛选出匹配数据
         });
      this.entity.typename = obj.name
    },
    servicechange(value){
      let obj = {};
         obj = this.ServiceList.find((item)=>{ //这里的Data就是上面遍历的数据源
           return item.Id === value; //筛选出匹配数据
         });
      this.entity.itemvalue = obj.ServiceName
    }
  }
}
</script>
